<template>
  <div class="app-container">
    <div class="menuquery">
      <el-button type="primary" @click="addOpen">添加</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      :row-style="rowStyle"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="menuname" label="名称"></el-table-column>
      <el-table-column prop="menupath" label="地址"></el-table-column>
      <el-table-column prop="component" label="组件"></el-table-column>
      <el-table-column prop="icon" label="标签"></el-table-column>
      <el-table-column prop="menutype" label="类型" :formatter="menuType" width="60px"></el-table-column>
      <el-table-column prop="flag" label="状态" :formatter="flagType" width="60px"></el-table-column>
      <el-table-column prop="menusort" label="序号" width="50px"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="primary" @click="modOpen(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改菜单" :visible.sync="modVisible" width="500px">
      <el-form :model="modparam" :rules="addrole" ref="modfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth" prop="menuname">
          <el-input v-model="modparam.menuname"></el-input>
        </el-form-item>
        <el-form-item label="地    址" :label-width="formLabelWidth" prop="menupath">
          <el-input v-model="modparam.menupath"></el-input>
        </el-form-item>
        <el-form-item label="组    件" :label-width="formLabelWidth" prop="component">
          <el-input v-model="modparam.component"></el-input>
        </el-form-item>
        <el-form-item label="序 号" :label-width="formLabelWidth" prop="component">
          <el-input-number v-model="modparam.menusort" :min="0" :max="20"></el-input-number>
        </el-form-item>
        <el-form-item label="标    签" :label-width="formLabelWidth" prop="icon">
          <el-input v-model="modparam.icon"></el-input>
        </el-form-item>
        <el-form-item label="父 菜 单" :label-width="formLabelWidth" prop="parentid">
          <el-select v-model="modparam.parentid" filterable placeholder="请选择">
            <el-option
              v-for="item in directory"
              :key="item.id"
              :label="item.menuname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类    型" :label-width="formLabelWidth" prop="menutype">
              <el-radio-group disabled v-model="modparam.menutype">
                <el-radio label="0">目录</el-radio>
                <el-radio label="1">菜单</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状    态" :label-width="formLabelWidth" prop="flag">
              <el-radio-group v-model="modparam.flag">
                <el-radio label="0">有效</el-radio>
                <el-radio label="1">无效</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button @click="closeMod">取 消</el-button>
        <el-button type="primary" @click="subMod">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加菜单" :visible.sync="addVisible" width="500px">
      <el-form :model="addparam" :rules="addrole" ref="addfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth" prop="menuname">
          <el-input v-model="addparam.menuname"></el-input>
        </el-form-item>
        <el-form-item label="地    址" :label-width="formLabelWidth" prop="menupath">
          <el-input v-model="addparam.menupath"></el-input>
        </el-form-item>
        <el-form-item label="组    件" :label-width="formLabelWidth" prop="component">
          <el-input v-model="addparam.component"></el-input>
        </el-form-item>
        <el-form-item label="序 号" :label-width="formLabelWidth" prop="menusort">
          <el-input-number v-model="addparam.menusort" :min="0" :max="20"></el-input-number>
        </el-form-item>
        <el-form-item label="标    签" :label-width="formLabelWidth" prop="icon">
          <el-input v-model="addparam.icon"></el-input>
        </el-form-item>
        <el-form-item label="父 菜 单" :label-width="formLabelWidth" prop="parentid">
          <el-select v-model="addparam.parentid" filterable placeholder="请选择">
            <el-option
              v-for="item in directory"
              :key="item.id"
              :label="item.menuname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类    型" :label-width="formLabelWidth" prop="menutype">
              <el-radio-group v-model="addparam.menutype">
                <el-radio label="0">目录</el-radio>
                <el-radio label="1">菜单</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状    态" :label-width="formLabelWidth" prop="flag">
              <el-radio-group v-model="addparam.flag">
                <el-radio label="0">有效</el-radio>
                <el-radio label="1">无效</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button @click="closeAdd">取 消</el-button>
        <el-button type="primary" @click="subAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { modify, del, add, allDirectory, alllist } from "@/api/menu";
import basemixni from "@/mixins/basemixin";

export default {
  mixins: [basemixni],
  data() {
    return {
      directory: [],
      addparam: {
        parentid: "",
        menuname: "",
        menupath: "",
        component: "",
        icon: "",
        menutype: "0",
        flag: "0",
        menusort: 0
      },
      modparam: {
        id: "",
        parentid: "",
        menuname: "",
        menupath: "",
        component: "",
        icon: "",
        menutype: "",
        flag: "",
        menusort: 0
      },
      addrole: {
        parentid: [{ required: true, message: "选择目录", trigger: "blur" }],
        menuname: [{ required: true, message: "输入名称", trigger: "blur" }],
        menupath: [{ required: true, message: "输入地址", trigger: "blur" }],
        icon: [{ required: true, message: "输入标签", trigger: "blur" }]
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      //查询所有菜单
      alllist().then(response => {
        this.list = response.data;
        this.listLoading = false;
      });
    },
    menuType(row) {
      return row.menutype === "0"
        ? "目录"
        : row.menutype === "1"
        ? "菜单"
        : "未知";
    },
    rowStyle({ row }) {
      if (row.menutype === "0") {
        return "background:#33FFCC";
      } else {
        return "";
      }
    },
    addOpen() {
      this.addVisible = true;
      allDirectory().then(response => {
        this.directory = response.data;
      });
    },
    subAdd() {
      this.$refs.addfrom.validate(valid => {
        if (valid) {
          add(this.addparam).then(response => {
            this.fetchData();
            this.closeAdd();
          });
        } else {
          return false;
        }
      });
    },
    modOpen(row) {
      this.modVisible = true;
      this.modparam = Object.assign({}, row);
      allDirectory().then(response => {
        this.directory = response.data;
      });
    },
    subMod() {
      this.$refs.modfrom.validate(valid => {
        if (valid) {
          modify(this.modparam).then(response => {
            this.fetchData();
            this.closeMod();
          });
        } else {
          return false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm("确定删除" + row.menuname, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del({ id: row.id }).then(response => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>


<style lang="scss">
.menuquery {
  margin: 10px 0 10px 0;
  float: right;
}
.menuadd {
  width: 110px;
}
</style>